﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class frm增加新商品 : Form
    {
        public frm增加新商品()
        {
            InitializeComponent();
        }
        string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
       //确定
        private void button1_Click(object sender, EventArgs e)
        {
            add();
        }

        public void xiala()
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string sql = "select * from [Commodity type]";
            DataSet ds = new DataSet("Student");
            SqlDataAdapter asdf = new SqlDataAdapter(sql, conn);
            asdf.Fill(ds, "Student");

            this.cboxia.DataSource = ds.Tables["Student"];
            this.cboxia.ValueMember = "SlId";
            this.cboxia.DisplayMember = "slLei";
            DataRow row = ds.Tables["Student"].NewRow();
            row["SlId"] = -1;
            row["slLei"] = "全部";
            ds.Tables["Student"].Rows.InsertAt(row, 0);
        }
        public void add()
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            try
            {
                int qw = this.cboxia.SelectedIndex;
                string sql = "insert into dbo.commodity values('" + Convert.ToInt32(this.txtbian.Text) + "','" + this.txtname.Text + "','" + qw + "','" + this.txtjia.Text + "')";
                SqlCommand comm = new SqlCommand(sql, conn);
                int i = comm.ExecuteNonQuery();
                if (i > 0)
                {
                    MessageBox.Show("添加成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }


        private void frm增加新商品_Load(object sender, EventArgs e)
        {
            xiala();
            this.cboxia.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
